<template>
	<view>
		<!-- 搜索区 -->
		<view class="search-box">
			<uni-search-bar :value="val" :radius="100"  @input="input" @click.enter="gotoGoodsList" cancelButton="none"></uni-search-bar>
		</view>
		
		
		<!-- 搜索建议区域 -->
		<ul class="button-content" v-if="searchResult.length !== 0">
			<li v-for="(item, i) in searchResult" :key="i" @click="gotoDetail(item)">
				<view class="button-content-text" url="">{{item.goods_name}}</view>
				<uni-icons type="forward" size="16"></uni-icons>
			</li>
		
		</ul>
		
		<!-- 搜索历史区域 -->
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons @click="delHistory" type="trash" size="17"></uni-icons>
			</view>	
			
			<view class="history-list">
				<uni-tag :text="item" v-for="(item, i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 防抖定时器
				timer: null,
				
				// 搜索关键词
				kw: '',
				
				// 搜索建议列表
				searchResult: [],
				
				// 搜索历史列表
				historyList: ['a', 'app', 'apple']
			};
		},
		onLoad () {
			this.historyList = JSON.parse(uni.getStorageSync("kw") || "[]")
		},
		methods: {
			input(e) {
				clearTimeout(this.timer)
				
				this.timer = setTimeout(()=>{
					this.kw = e.value
					
					this.getSearchList()
				
					
				}, 500)
				// this.historyList.push(this.kw)
				const set = new Set(this.historyList)

				set.delete(this.kw)
				set.add(this.kw)
				
				this.historyList = Array.from(set)

				uni.setStorageSync("kw", JSON.stringify(this.historyList))
			},
			async getSearchList() {
				if (this.kw == "") {
					this.searchResult = []
					return
				}
				
				const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.kw})
				this.searchResult = res.message
				
			},
			
			// 跳转详情页
			gotoDetail(item) {
				uni.navigateTo({
					url: "/subpkg/goods_detail/goods_detail?goods_id=" + item.goods_id
				})
			},
			
			gotoGoodsList() {
				uni.navigateTo({
					url: '//subpkg/goods_list/goods_list'
				})
			},
			
			// 清除历史记录
			delHistory() {
				const that = this
				uni.showModal({
					title: '是否清除历史记录',
					showCancel: true,
					success: function(res) {
						if (res.confirm) {
							
							that.historyList = []
							uni.removeStorageSync("kw")
						}
					}
				})
				
			},
			
			gotoGoodsList(kw) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + kw
				})
			}
		},
		computed: {
			histories () {
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		
		z-index: 9999;
	}
	.button-content {
		padding: 0 5px;
		li {
			border-bottom: 1px solid #efefef;
			padding: 13px 0;
			font-size: 14px;
			display: flex;
			align-items: center;
			justify-content: space-between;
			.button-content-text {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
			
		}
	}
	
	.history-box {
		padding: 0 5px;
		.history-title {
			display: flex;
			align-items: center;
			height: 40px;
			justify-content: space-between;
			border-bottom: 1px solid #efefef;
		}
		.history-list {
			display: flex;
			.uni-tag {
				margin-top: 5px;
				margin-left: 5px;
			}
			
		}
	}
</style>
